package com.runjian.rundo.iot.agent.iec104.parser.frame.monitoring.process;

import com.runjian.rundo.iot.agent.iec104.parser.frame.ASDUFrame;
import com.runjian.rundo.iot.agent.iec104.parser.frame.Cp56Time2a;
import com.runjian.rundo.iot.agent.iec104.parser.frame.InfoElement;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ByteUtil;
import com.runjian.rundo.iot.agent.iec104.parser.frame.monitoring.base.BaseMonitoringFrame;
import lombok.Getter;
import lombok.Setter;

/**
 * M_SP_TB_1(30, "(遥信)单点信息,带时标CP56Time2a的单点信息")
 *
 * @author CaoQingHua
 */
@Getter
@Setter
public class M_SP_TB_1 extends BaseMonitoringFrame {

    @Override
    public void analysisInfo(byte[] msg) {
        // 带时标的单双点遥信
        if (this.getSq()) {
            // 按照DL/T 634.5104-2009规定，带长时标的单/双点信息遥信报文并不存在信息元素序列（SQ=1）的情况

        } else {
            for (int i = 0; i < this.getNumber(); i++) {
                this.addInfoElement(InfoElement.<Short>builder()
                    .address(ByteUtil.bytesToInt(new byte[] {msg[12 + (11 * i)], msg[13 + (11 * i)], msg[14 + (11 * i)], 0x00}))
                    .value((short)(msg[15 + (11 * i)] & 0xff)).date(Cp56Time2a.of(ArrayUtil.sub(msg, 16 + i * 11, 23 + i * 11))).build());
            }
        }
    }

}
